<script>
	export default {
		onLaunch: function() {
			if (uni.getStorageSync('token')) {
				this.$myRequest('mine/mine', {}).then((res) => {
					this.$store.commit('setUserInfo', res.data)
				})

			} else {
				this.getOpenId()
			}
		},
		onShow: function() {
			setInterval(() => {
				if (this.$store.state.user_info && this.$store.state.user_info.cart_num > 0) {
					uni.setTabBarBadge({ //显示数字  
						index: 2, //tabbar下标
						text: this.$store.state.user_info.cart_num.toString() //数字
					})
				} else {
					uni.removeTabBarBadge({
						index: 1
					});
				}
			}, 1000); // 每隔5秒执行一次
		},
		onHide: function() {},

		methods: {
			/**
			 * 前端获取openId
			 */
			getOpenId() {
				let that = this
				uni.login({
					success: res => {
						//code值(5分钟失效)
						let code = res.code;
						that.$myRequest('login/xcx', {
								code: code
							}, 'POST')
							.then((res) => {
								that.$store.commit('setUserInfo', res.data)
								uni.setStorageSync('token', res.data.token)
							})
					}
				});
			},
		}
	}
</script>
<style>
	[v-cloak] {
		display: none;
	}

	page {
		font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue",
			"PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei",
			Arial, sans-serif;
		font-size: 32upx;
		color: #333;
		background: #f5f5f5;
		letter-spacing: 0.034em;
	}

	image {
		height: auto;
	}
</style>